/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.cancel.postcancel;

import cn.hutool.core.util.StrUtil;
import com.lxk.event.helper.EventHelper;
import com.lxk.event.oms.OrderOptionLogEvent;
import com.lxk.event.oms.enums.OrderEnums;
import com.lxk.oms.flow.cancel.CancelOrderContext;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.oms.postorder.domain.vo.OmsPostOrderItemVo;
import com.lxk.service.core.Result;
import com.lxk.service.pms.api.PmsServiceApi;
import com.lxk.service.pms.request.ProductSkuUpdateStockParam;
import com.lxk.service.pms.response.SkuUpdateStockResult;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import org.dromara.common.core.exception.ServiceException;

import java.util.List;
import java.util.stream.Collectors;

import static com.lxk.oms.flow.cancel.postcancel.FlowPostOrderCancelOrderNodeConst.POST_ORDER_STOCK_ROLLBACK_CANCEL_ORDER_NODE;
import static com.lxk.service.core.Result.SUCCESS;

/**
 * 库存回滚
 *
 * @author heyi
 * 2024/9/21
 */
@AllArgsConstructor
@LiteflowComponent(POST_ORDER_STOCK_ROLLBACK_CANCEL_ORDER_NODE)
public class StockRollBackNode extends NodeComponent {
    private final PmsServiceApi pmsServiceApi;

    @Override
    public void process() throws Exception {
        PostCancelOrderContext contextBean = this.getContextBean(PostCancelOrderContext.class);
        List<OmsPostOrderItemVo> itemVoList = contextBean.getPostOrderItemList();
        ProductSkuUpdateStockParam param = new ProductSkuUpdateStockParam();
        List<ProductSkuUpdateStockParam.Item> itemList = itemVoList.stream().map(item -> {
            ProductSkuUpdateStockParam.Item i = new ProductSkuUpdateStockParam.Item();
            i.setName(StrUtil.format("{}-{}", item.getSpuName(), item.getSkuName()));
            i.setId(item.getSkuId());
            i.setCount(item.getRefundCount());
            return i;
        }).collect(Collectors.toList());
        param.setItems(itemList);
        Result<SkuUpdateStockResult> skuStock =
            pmsServiceApi.addSkuStock(param);
        if (skuStock.getCode() != SUCCESS) {
            throw new ServiceException(skuStock.getMsg());
        }
        sendSkuRollBackEvent(contextBean, param);
    }

    private void sendSkuRollBackEvent(PostCancelOrderContext contextBean, ProductSkuUpdateStockParam param) {
        OrderOptionLogEvent optionLogEvent = new OrderOptionLogEvent();
        OmsOrderVo omsOrderVo = contextBean.getOmsOrderVo();
        optionLogEvent.setOrderSn(omsOrderVo.getOrderNo());
        optionLogEvent.setUserId(contextBean.getUserId());
        optionLogEvent.setOptionUserId(contextBean.getOptionUserId());
        optionLogEvent.setOptionUserType(contextBean.getOptionUserType());
        optionLogEvent.setContent(OrderOptionLogEvent.OrderContentFormatEnums.STOCK_ORDER.formatContent(param.info()));
        optionLogEvent.setUserType(contextBean.getUserType());
        optionLogEvent.setMerId(omsOrderVo.getMerId());
        optionLogEvent.setOrderLogEnums(OrderOptionLogEvent.OrderLogEnums.ORDER);
        optionLogEvent.setTenantId(contextBean.getTenantId());
        EventHelper.publishEvent(optionLogEvent);
    }

    @Override
    public boolean isAccess() {
        CancelOrderContext contextBean = this.getContextBean(CancelOrderContext.class);
        OmsOrderVo omsOrderVo = contextBean.getOmsOrderVo();
        return contextBean.getSuccess() && OrderEnums.NeedStock.YES.getCode().equals(omsOrderVo.getNeedStock());
    }
}
